package com.xidian.zhiancloud.controller.equipFacility.ohManager;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.xidian.zhiancloud.controller.base.BaseController;
import com.xidian.zhiancloud.dto.ResultDTO;
import com.xidian.zhiancloud.enums.GeneralResponseEnums;
import com.xidian.zhiancloud.model.OhRegister;
import com.xidian.zhiancloud.qo.equipFacility.OhRegisterQO;
import com.xidian.zhiancloud.service.equipFacility.OhRegisterService;
import com.xidian.zhiancloud.vo.equipFacility.OhRegisterVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.Calendar;
import java.util.Date;

/**
 * 设备设施 检修管理 检修登记接口
 *
 * @Author: 任启亮
 * @Date: 2020/11/11
 */
@RestController
@RequestMapping("/OhRegister")
public class OhRegisterController extends BaseController {
    @Autowired
    private OhRegisterService ohRegisterService;

    /**
     * 增加检修登记记录
     *
     * @param ohRegister 检修登记对象
     * @return 返回是否添加成功
     */
    @PostMapping("/add")
    public ResultDTO<Boolean> add(OhRegister ohRegister) {

        ohRegister.setCompanyId(getSessionUserCompanyId());
        ohRegister.setCreator(getSessionUserid());
        ohRegister.setCreateTime(new Date(System.currentTimeMillis()));

        Boolean result = ohRegisterService.add(ohRegister);
        return ResultDTO.successOf(result, GeneralResponseEnums.SUCCESS);
    }

    /**
     * 根据id获取检修登记记录
     *
     * @param id 检修登记记录对象
     * @return 检修登记对象
     */
    @GetMapping("/getById")
    public ResultDTO<OhRegister> getById(@RequestParam("id") Long id) {
        return ResultDTO.successOf(ohRegisterService.getById(id), GeneralResponseEnums.SUCCESS);
    }

    /**
     * 根据id删除检修登记记录
     *
     * @param ids ids数组
     * @return 返回删除标志
     */
    @PostMapping("/deleteByIds")
    public ResultDTO<Boolean> deleteByIds(Long[] ids) {
        Boolean result = ohRegisterService.removeByIds(Arrays.asList(ids));
        return ResultDTO.successOf(result, GeneralResponseEnums.SUCCESS);
    }

    /**
     * 根据id修改检修登记记录
     *
     * @param ohRegister 检修登记对象
     * @return 返回修改状态
     */
    @PostMapping("/patchById")
    public ResultDTO<Boolean> patchById(OhRegister ohRegister) {
        ohRegister.setCompanyId(null);
        Boolean result=ohRegisterService.updateById(ohRegister);
        return ResultDTO.successOf(result,GeneralResponseEnums.SUCCESS);
    }

    /**
     * @param ohRegisterQO 传递参数
     * @return 返回列表
     */
    @GetMapping("/getListByQO")
    public ResultDTO<IPage<OhRegisterVO>> getListByQO(OhRegisterQO ohRegisterQO) {
        if (ohRegisterQO.getCurrent()== null || ohRegisterQO.getSize() == null) {
            return ResultDTO.errorOf(GeneralResponseEnums.REQUEST_ERROR);
        }
        ohRegisterQO.setCompanyId(getSessionUserCompanyId());
        return ResultDTO.successOf(ohRegisterService.getListByQO(ohRegisterQO), GeneralResponseEnums.SUCCESS);
    }

    /**
     * 提交
     *
     * @param
     * @return 是否提交成功
     */
    @PostMapping("/submitById")
    public ResultDTO<Boolean> submitById(Long id) {
        return ResultDTO.successOf(ohRegisterService.submitById(id), GeneralResponseEnums.SUCCESS);
    }
}
